﻿#region License

// Droid Manager - An Android Phone Tools Suite
// Copyright (C) 2011
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DroidManager.Android;
using DroidManager.Data.Utils;
using DroidManager.Log;
using Managed.Adb;
using DroidManager.Plugins;

namespace DroidManager.UI
{
    internal partial class Main : Form
    {
        /// <summary>
        /// Logger
        /// </summary>
        private Logger _log = Logger.Instance;

        /// <summary>
        /// Device Manager
        /// </summary>
        private DeviceManager _dm = DeviceManager.Instance;

        /// <summary>
        /// Plugin Engine
        /// </summary>
        private PluginEngine _pe = PluginEngine.Instance;

        /// <summary>
        /// Main Settings Form
        /// </summary>
        private MainSettings _ms = new MainSettings();

        /// <summary>
        /// Device Info Form
        /// </summary>
        private DeviceInfo _di = new DeviceInfo();

        /// <summary>
        /// About Form
        /// </summary>
        private About _ab = new About();

        public Main()
        {
            InitializeComponent();
            versionLbl.Text = "Version " + _ab.AssemblyVersion;
            InitializeApp();
        }

        private void InitializeApp()
        {
            UpdatePluginList();
            UpdateDeviceList(true);
            refreshTmr.Start();
        }

        private string GetAndroidVersion(int sdklevel)
        {
            switch (sdklevel)
            {
                case 1:
                    return "android 1.0";
                case 2:
                    return "android 1.1";
                case 3:
                    return "android 1.5";
                case 4:
                    return "android 1.6";
                case 5:
                    return "android 2.0";
                case 6:
                    return "android 2.0.1";
                case 7:
                    return "android 2.1";
                case 8:
                    return "android 2.2";
                case 9:
                    return "android 2.3";
                case 10:
                    return "android 2.3.3";
                case 11:
                    return "android 3.0";
                default:
                    return "unknown ver";
            }
        }

        private void UpdatePluginList()
        {
            int i = 0;
            foreach (Plugin plugin in _pe.Plugins)
            {
                if (plugin.ShowInMenu)
                {
                    if (plugin.Icon != null)
                    {
                        this.pluginIcons.Images.Add(plugin.Icon);
                        pluginList.Items.Add(plugin.Name, i);
                    }
                    else
                    {
                        pluginList.Items.Add(plugin.Name);
                    }
                    i++;
                }
            }
        }

        /// <summary>
        /// Refresh device list
        /// </summary>
        private void UpdateDeviceList(bool ignore_count)
        {
            if (ignore_count || deviceList.Items.Count != _dm.Devices.Count)
            {
                deviceList.DataSource = _dm.Devices;
                deviceList.DisplayMember = "Name";
            }
        }

        private void CloseApp()
        {
            _log.Info("Closing App");
            _dm.Stop();
            Environment.Exit(0);
        }

        private void ShowDeviceInfo(Device device)
        {
            if (device != null)
            {
                _di.ShowDialog(device);
            }
        }

        #region EventHandlers

        #region EditBtn

        private void editBtn_MouseUp(object sender, MouseEventArgs e)
        {
            editBtn.BackgroundImage = Properties.Resources.edit_normal;
            if (deviceList.SelectedItem == null)
            {
                MessageBox.Show("You must select a device to rename!", "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            else
            {
                string currentName = deviceList.Text;
                if (Generic.InputBox("New Name", "Insert New Name", ref currentName) == DialogResult.OK)
                {
                    _dm.ChangeDeviceName((Device) deviceList.SelectedItem, currentName);
                    UpdateDeviceList(true);
                }
            }
        }

        private void editBtn_MouseDown(object sender, MouseEventArgs e)
        {
            editBtn.BackgroundImage = Properties.Resources.edit_click;
        }

        private void editBtn_MouseEnter(object sender, EventArgs e)
        {
            editBtn.BackgroundImage = Properties.Resources.edit_hover;
        }

        private void editBtn_MouseLeave(object sender, EventArgs e)
        {
            editBtn.BackgroundImage = Properties.Resources.edit_normal;
        }

        #endregion

        #region CloseBtn

        private void closeBtn_MouseUp(object sender, MouseEventArgs e)
        {
            closeBtn.BackgroundImage = Properties.Resources.close_normal;
            this.Close();
        }

        private void closeBtn_MouseDown(object sender, MouseEventArgs e)
        {
            closeBtn.BackgroundImage = Properties.Resources.close_click;
        }

        private void closeBtn_MouseEnter(object sender, EventArgs e)
        {
            closeBtn.BackgroundImage = Properties.Resources.close_hover;
        }

        private void closeBtn_MouseLeave(object sender, EventArgs e)
        {
            closeBtn.BackgroundImage = Properties.Resources.close_normal;
        }

        #endregion

        #region MinimizeBtn

        private void minimizeBtn_MouseUp(object sender, MouseEventArgs e)
        {
            minimizeBtn.Image = Properties.Resources.minimize_normal;
            this.Hide();
            alphaFormTransformer1.Hide();
        }

        private void minimizeBtn_MouseDown(object sender, MouseEventArgs e)
        {
            minimizeBtn.Image = Properties.Resources.minimize_click;
        }

        private void minimizeBtn_MouseEnter(object sender, EventArgs e)
        {
            minimizeBtn.Image = Properties.Resources.minimize_hover;
        }

        private void minimizeBtn_MouseLeave(object sender, EventArgs e)
        {
            minimizeBtn.Image = Properties.Resources.minimize_normal;
        }

        #endregion

        #region SettingsBtn

        private void settingsBtn_MouseUp(object sender, MouseEventArgs e)
        {
            settingsBtn.BackgroundImage = Properties.Resources.settings_normal;
            _ms.ShowDialog();
        }

        private void settingsBtn_MouseDown(object sender, MouseEventArgs e)
        {
            settingsBtn.BackgroundImage = Properties.Resources.settings_click;
        }

        private void settingsBtn_MouseEnter(object sender, EventArgs e)
        {
            settingsBtn.BackgroundImage = Properties.Resources.settings_hover;
        }

        private void settingsBtn_MouseLeave(object sender, EventArgs e)
        {
            settingsBtn.BackgroundImage = Properties.Resources.settings_normal;
        }

        #endregion

        private void versionLbl_Click(object sender, EventArgs e)
        {
            _ab.ShowDialog();
        }

        private void deviceList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (deviceList.SelectedItem != null)
                {
                    deviceInfoPanel.Show();
                    Device device = (Device) deviceList.SelectedItem;
                    deviceNameLbl.Text = device.Name;
                    if (deviceNameLbl.Text.Length > 9)
                    {
                        deviceNameLbl.Text = deviceNameLbl.Text.Replace(deviceNameLbl.Text.Substring(9), "...");
                    }
                    deviceVerLbl.Text =
                        GetAndroidVersion(Convert.ToInt32(device.GetProperty(Device.PROP_BUILD_API_LEVEL)));
                }
                else
                {
                    deviceInfoPanel.Hide();
                }
            }
            catch (KeyNotFoundException)
            {
                deviceVerLbl.Text = "Unknown Version";
            }
        }

        private void Main2_Load(object sender, EventArgs e)
        {
            alphaFormTransformer1.TransformForm();
        }

        private void Main2_Closed(object sender, EventArgs e)
        {
            CloseApp();
        }

        private void refreshTmr_Tick(object sender, EventArgs e)
        {
            UpdateDeviceList(false);
        }

        private void deviceIcon_Click(object sender, EventArgs e)
        {
            ShowDeviceInfo((Device) deviceList.SelectedItem);
        }

        private void pluginList_DoubleClick(object sender, EventArgs e)
        {
            Plugin plugin = _pe.GetPluginByName(pluginList.SelectedItems[0].Text);
            if (plugin != null)
            {
                plugin.Start((Device) deviceList.SelectedItem, null);
            }
        }

        private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
            alphaFormTransformer1.Show();
        }

        private void showMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            alphaFormTransformer1.Show();
        }

        private void quitMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion
    }
}